package com.hotbrains.DateAppWidget;

import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.content.*;
import android.util.Log;

/**
 * SharedPreference에 데이터를 처리하는 Model 클래스 
 * DDMS의 File Explorer /data/data/com.hotbrains.DataAppWidget/shared_prefs/com.hotbrains.DataAppWidget.xml 파일에 저장됨.
 * 앱위젯을 추가/삭제하며 이 데이터가 변경되는 것을 확인할 수 있음.
 * 
 * com.hotbrains.DateAppWidget
 * DateAppWidgetModel.java
 *
 * @author junhwan_kim
 * @date 2011. 4. 29. 오전 11:47:46
 * @Version
 */
public class WidgetPreferences {
	private static final String TAG = "MyWatch";
	
	private static final String KEY_WIDGETID = "widgetid";
	private static final String KEY_IMAGEPATH = "imagepath";
	private static final String KEY_AMPM = "ampm";
	private static final String KEY_DATE = "date";
	private static final String KEY_DAYOFWEEK = "dayofweek";
	private static final String KEY_BACKGROUND = "background";
	private static final String KEY_ANIMATION = "animation";
	
	public static final int WIDGETID = 0;
	public static final int IMAGEPATH = 1;
	public static final int AMPM = 2;
	public static final int DATE = 3;
	public static final int DAYOFWEEK = 4;
	public static final int BACKGROUND = 5;
	public static final int ANIMATION = 6;
	
	/**
	 * 해당 widgetId의 widgetId를 저장한다.
	 * 
	 * @Method setWidgetId
	 * @param context
	 * @param widgetId
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:17
	 */
	static public void setWidgetId(Context context, String widgetId) {
		SharedPreferences.Editor editor = context.getSharedPreferences(context.getPackageName(), 0).edit();
		editor.putString(widgetId, widgetId);
		editor.commit();
	}
	
	/**
	 * onRecieve에서 TIME_TICK일 때, 모든 widget을 update해주기 위함.
	 * 
	 * @Method getAllWidgetId
	 * @param context
	 * @return
	 *
	 * @author jun2yah
	 * @date 2011. 5. 6. 오후 3:25:39
	 */
	static public Map<String,?> getAllWidgetId(Context context) {
		SharedPreferences pref = context.getSharedPreferences(context.getPackageName(), 0);
		Map<String,?> id = pref.getAll();
		return id;
	}
	
	/**
	 * 해당 widgetId의 Preference를 제거한다.
	 * 
	 * @Method removeWidgetId
	 * @param context
	 * @param key
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:27
	 */
	static public void removeWidgetId(Context context, int widgetId) {
		SharedPreferences.Editor editor = context.getSharedPreferences(context.getPackageName(), 0).edit();
		editor.remove(String.valueOf(widgetId));
		editor.commit();
	}
	
	/**
	 * 모든 환경 설정 항목을 제거한다.
	 * 
	 * @Method removeAllWidgetId
	 * @param context
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:31
	 */
	static public void removeAllWidgetId(Context context) {
		SharedPreferences.Editor editor = context.getSharedPreferences(context.getPackageName(), 0).edit();
		editor.clear();
		editor.commit();
	}
	
	/**
	 * widgetID별로 설정을 SharedPreference에 저장한다.
	 * 
	 * @Method setPref
	 * @param prefValue
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:17
	 */
	static public void setPref(Context context, String[] prefValue) {
		Log.i(TAG, "setPref = [" + 
				prefValue[WIDGETID] + "][" + 
				prefValue[IMAGEPATH] + "][" + 
				prefValue[AMPM] + "][" + 
				prefValue[DATE] + "][" + 
				prefValue[DAYOFWEEK] + "][" + 
				prefValue[BACKGROUND] + "][" + 
				prefValue[ANIMATION] + "]");
		
		SharedPreferences pref = context.getSharedPreferences("setting_" + prefValue[WIDGETID] + ".xml", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(KEY_WIDGETID, prefValue[WIDGETID]);
		editor.putString(KEY_IMAGEPATH, prefValue[IMAGEPATH]);
		editor.putString(KEY_AMPM, prefValue[AMPM]);
		editor.putString(KEY_DATE, prefValue[DATE]);
		editor.putString(KEY_DAYOFWEEK, prefValue[DAYOFWEEK]);
		editor.putString(KEY_BACKGROUND, prefValue[BACKGROUND]);
		editor.putString(KEY_ANIMATION, prefValue[ANIMATION]);
		editor.commit();
	}
	
	/**
	 * 해당 widgetID의 환경설정 항목을 가져온다.
	 * 
	 * @Method getPref
	 * @param context
	 * @param key
	 * @return
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:23
	 */
	static public String[] getPref(Context context, int widgetId) {
		SharedPreferences pref = context.getSharedPreferences("setting_" + widgetId + ".xml", 0);
		
		String[] prefValue = {pref.getString(KEY_WIDGETID, ""),
				pref.getString(KEY_IMAGEPATH, ""),
				pref.getString(KEY_AMPM, ""),
				pref.getString(KEY_DATE, ""),
				pref.getString(KEY_DAYOFWEEK, ""),
				pref.getString(KEY_BACKGROUND, ""),
				pref.getString(KEY_ANIMATION, "")};
		
		Log.i(TAG, "getPref = [" + 
				prefValue[WIDGETID] + "][" + 
				prefValue[IMAGEPATH] + "][" + 
				prefValue[AMPM] + "][" + 
				prefValue[DATE] + "][" + 
				prefValue[DAYOFWEEK] + "][" + 
				prefValue[BACKGROUND] + "][" + 
				prefValue[ANIMATION] + "]");
		
		return prefValue;
	}
	
	/**
	 * 해당 Key의 환경설정 항목을 제거한다.
	 * 
	 * @Method removePref
	 * @param context
	 * @param key
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:27
	 */
	static public void removePref(Context context, int widgetId) {
		SharedPreferences.Editor editor = context.getSharedPreferences("setting_" + widgetId + ".xml", 0).edit();
		editor.remove(String.valueOf(widgetId));
		editor.commit();
	}
	
	/**
	 * 모든 환경 설정 항목을 제거한다.
	 * 
	 * @Method removeAllPref
	 * @param context
	 *
	 * @author junhwan_kim
	 * @date 2011. 4. 29. 오전 11:47:31
	 */
	static public void removeAllPref(Context context) {
		Map<String,?> map = getAllWidgetId(context);	// widget id를 map으로 가져온다.
        Set<String> set = map.keySet();		// hm의 키값들을 Set(집합)으로 값을 가져옵니다.
        Object []hmKeys = set.toArray();	// Set의 값들을 Object 배열로 변환합니다.
        
        // Object 배열의 요소(키값이죠.)이걸로 hm의 값을 가져와서 출력합니다.
        for(int i =  0; i < hmKeys.length; i++) {
            String key = (String)hmKeys[i];
            SharedPreferences.Editor editor = context.getSharedPreferences("setting_" + key + ".xml", 0).edit();
    		editor.clear();
    		editor.commit();
        }
	}
}